const vm = new Vue({
    el: '#main',
    data: {
        houseId: fxj.getSearch().get('houseId'),
        monthList: [], // 显示的日历
        daysList: [], // day线性链表
        selectedDays: [],
    },
    methods: {
        selectDay,
        clearSelectedDays,
        changePrice,
    },
});

function apiready() {
    fxj.fixLayout();
    reqDateList();
    initActionSheet();
}

async function reqDateList() {
    const today = new Date();
    const res = await ajax({
        url: 'home/houseDetail/getTwelveMonthPriceStatus',
        data: {
            houseId: vm.houseId,
            time: `${today.getFullYear()}/${today.getMonth()}/1`,
        },
    });
    clearSelectedDays();
    if (res.err === 0) {
        const tempList = [];
        let daysListIndex = 0;
        forEach(res.data, month => {
            forEach(month, day => {
                day.year = day.date.slice(0, 4);
                day.month = +day.date.slice(4, 6);
                day.day = +day.date.slice(6, 8);
                day.date = `${day.year}/${day.month}/${day.day}`;
                day.isOverdue = new Date(day.date) - new Date(`${today.getFullYear()}/${today.getMonth() + 1}/${today.getDate()}`);
                day.selected = false;
                day.index = daysListIndex;
                vm.daysList.push(day);
                daysListIndex += 1;
            });
            const date = new Date(month[0].date);
            tempList.push({
                title: `${date.getFullYear()}.${Number(date.getMonth()) + 1}`,
                firstDayWeek: Number(month[0].week),
                dayList: month,
            });
        });
        vm.monthList = tempList;
    }
}

function selectDay(day) {
    day.selected = !day.selected;
    vm.selectedDays = getSelectedDay();
}


function getSelectedDay() {
    return vm.daysList.filter(day => {
        if (day.selected) {
            return day;
        }
        return false;
    });
}

function clearSelectedDays() {
    forEach(vm.selectedDays, day => {
        day.selected = false;
    });
    vm.selectedDays = [];
}

async function changePrice() {
    const changePrice = await fxj.dialog.prompt({msg: '请输入金额', type: 'number'});
    if (!changePrice) return;
    const close = fxj.dialog.loading({noBg: true});
    const selectDate = vm.selectedDays.map(day => {
        return `${day.year}-${day.month}-${day.day}`;
    });
    const res = await ajax({
        url: 'home/houseManage/housePriceStatus',
        data: {
            houseId: vm.houseId,
            selectDate,
            changePrice,
        },
    });
    close();
    mui.toast(res.msg);
    reqDateList();
}

async function lockHandle(type) {
    const close = fxj.dialog.loading({noBg: true});
    const selectDate = vm.selectedDays.map(day => {
        return `${day.year}-${day.month}-${day.day}`;
    });
    const res = await ajax({
        url: 'home/houseManage/housePriceStatus',
        data: {
            houseId: vm.houseId,
            selectDate,
            changeStatus: type,
        },
    });
    close();
    toast(res.msg);
    reqDateList();
}

async function initActionSheet() {
    document.getElementById('changeRoomState').addEventListener('click', () => {
        api.actionSheet({
            title: '',
            cancelTitle: '取消',
            buttons: ['锁定', '解锁'],
        }, function (ret, err) {
            const index = ret.buttonIndex;
            if (index === 1) {
                lockHandle('2');
            }
            else if (index === 2) {
                lockHandle('0');
            }
        });
    });
}